package com.ruoyi.business.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.business.vo.TContactInfoVo;
import com.ruoyi.business.service.ITContactInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 联系方式Controller
 *
 * @author Tellsea
 * @date 2024-01-17
 */
@Api(tags = "联系方式Controller")
@RestController
@RequestMapping("/business/contactInfo")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TContactInfoController extends BaseController {

    private final ITContactInfoService tContactInfoService;

    private static final Logger log = LoggerFactory.getLogger(TContactInfoController.class);

    @ApiOperation("查询联系方式列表")
    @PreAuthorize("@ss.hasPermi('business:contactInfo:list')")
    @GetMapping("/list")
    public TableDataInfo<TContactInfoVo> list(TContactInfoVo entity) {
        return tContactInfoService.queryList(entity);
    }

    @ApiOperation("查询联系方式所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(TContactInfoVo entity) {
        return AjaxResult.success("查询成功", tContactInfoService.queryAll(entity));
    }

    @ApiOperation("导出联系方式列表")
    @PreAuthorize("@ss.hasPermi('business:contactInfo:export')")
    @Log(title = "联系方式", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TContactInfoVo entity) {
        List<TContactInfoVo> list = tContactInfoService.queryAll(entity);
        ExcelUtil<TContactInfoVo> util = new ExcelUtil<>(TContactInfoVo.class);
        util.exportExcel(response, list, "联系方式数据");
    }

    @ApiOperation("获取联系方式详细信息")
    //@PreAuthorize("@ss.hasPermi('business:contactInfo:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", tContactInfoService.queryById(id));
    }

    @ApiOperation("接受推送数据")
    //@PreAuthorize("@ss.hasPermi('business:contactInfo:query')")
    @PostMapping(value = "/acceptData")
    public AjaxResult acceptData(@RequestBody TContactInfoVo entity) {
        log.info("--------------------经度:" + entity.getLongitude() + "纬度:" + entity.getLatitude() + "----------------------错误信息:" + entity.getErrMsg());
        return AjaxResult.success("推送成功");
    }

    @ApiOperation("新增联系方式")
    @PreAuthorize("@ss.hasPermi('business:contactInfo:add')")
    @Log(title = "联系方式", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody TContactInfoVo entity) {
        return toAjax(tContactInfoService.save(entity));
    }

    @ApiOperation("修改联系方式")
    @PreAuthorize("@ss.hasPermi('business:contactInfo:edit')")
    @Log(title = "联系方式", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody TContactInfoVo entity) {
        return toAjax(tContactInfoService.updateById(entity));
    }

    @ApiOperation("删除联系方式")
    @PreAuthorize("@ss.hasPermi('business:contactInfo:remove')")
    @Log(title = "联系方式", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tContactInfoService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    @GetMapping("/health")
    public JSONObject health() {
        JSONObject result = new JSONObject();
        result.put("success",true);
        result.put("message","服务正常");
        return result;
    }

    @GetMapping("/getServerTime")
    public AjaxResult getServerTime() {
        return AjaxResult.success("当前时间", DateUtils.getTime());
    }
}

